package com.lcxw.hxzbadmin.controller.chargecontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.pojo.ChargeConfig;
import com.lcxw.hxzbadmin.service.baseservice.ChargeConfigService;
import com.lcxw.hxzbapi.entity.response.Response;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class ChargeConfigController {
    private Logger logger = LoggerFactory.getLogger(ChargeConfigController.class);

    @Resource
    ChargeConfigService chargeConfigService;

    @PostMapping("/charge/config")
    public Response updateChargeConfig(@RequestBody JSONObject requestParams){
        String adminName = requestParams.getString("adminName");
        logger.info("admin {} update chargeConfig.",adminName);

        String chargeType = requestParams.getString("chargeType");
        String account = requestParams.getString("account");
        String secret = requestParams.getString("secret");

        if (!chargeType.equals("Alipay") && !chargeType.equals("WeChat")){
            return Response.common(logger,"chargeType must be Alipay or WeChat.", ErrorCode.PARAM_ERROR,"chargeType must be Alipay or WeChat",null);
        }

        ChargeConfig chargeConfig = new ChargeConfig();
        chargeConfig.setChargeType(chargeType);
        chargeConfig.setAccount(account);
        chargeConfig.setSecret(secret);

        try {
            chargeConfigService.updateChargeByType(chargeConfig);
        } catch (Exception e) {
            return Response.dbFail(logger,"update chargeConfig"+chargeConfig.toString());
        }

        return Response.common(logger,"update chargeConfig success"+chargeConfig.toString(),0,"success",null);

    }

}
